package com.lingshi.shopping_manager_api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingshi.common.entity.Specification;
import com.lingshi.common.entity.SpecificationOption;
import com.lingshi.common.entity.SpecificationOptions;
import com.lingshi.common.result.BaseResult;
import com.lingshi.common.service.SpecificationService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品规格
 */
@RestController
@RequestMapping("/specification")
public class SpecificationController {
    @DubboReference
    private SpecificationService specificationService;

    /**
     * 新增商品规格
     *
     * @param specification 商品规格
     * @return 执行结果
     */
    @PostMapping("/add")
    public BaseResult add(@RequestBody Specification specification) {

        specificationService.add(specification);
        return BaseResult.success();
    }

    /**
     * 修改商品规格
     *
     * @param specification 商品规格
     * @return 执行结果
     */
    @PutMapping("/update")
    public BaseResult update(@RequestBody Specification specification) {

        specificationService.update(specification);
        return BaseResult.success();
    }

    /**
     * 删除商品规格
     *
     * @param ids 商品规格id集合
     * @return 执行结果
     */
    @DeleteMapping("/delete")
    public BaseResult delete(Long[] ids) {
        specificationService.delete(ids);
        return BaseResult.success();
    }
    /**
     * 根据id查询商品规格
     * @param id 商品规格id
     * @return 查询结果
     */
    @GetMapping("/findById")
    public BaseResult findById(Long id) {
        Specification specification = specificationService.findById(id);
        return BaseResult.success(specification);
    }

    /**
     * 分页查询商品规格
     *
     * @param page 页码
     * @param size 每页条数
     * @return 查询结果
     */
    @GetMapping("/search")
    public BaseResult search(@RequestParam(defaultValue = "1")int page,
                             @RequestParam(defaultValue = "10")int size) {
        Page<Specification> page1 =
                specificationService.search(page, size);
        return BaseResult.success(page1);
    }

    /**
     * 查询某种商品类型下的所有规格
     *
     * @param id 商品类型id
     * @return 查询结果
     */
    @GetMapping("/findByProductTypeId")
    public BaseResult findByProductTypeId(Long id) {
        List<Specification> specifications = specificationService.findByProductTypeId(id);
        return BaseResult.success(specifications);
    }

    /**
     * 新增商品规格项
     *
     * @param specificationOptions 商品规格项集合
     * @return 执行结果
     */
    @PostMapping("/addOption")
    public BaseResult addOption(@RequestBody SpecificationOptions specificationOptions) {

        specificationService.addOption(specificationOptions);
        return BaseResult.success();
    }

    /**
     * 删除商品规格项
     *
     * @param ids 商品规格项id集合
     * @return 执行结果
     */
    @DeleteMapping("/deleteOption")
    public BaseResult deleteOption(Long[] ids) {
        specificationService.deleteOption(ids);
        return BaseResult.success();
    }
}
